
$(function () {
    getPermission();
})
function editRole(data, index) {
    layui.use(['upload', 'form', 'laydate'], function () { //此段代码必不可少
        var form = layui.form;
        var upload = layui.upload;
        var laydate = layui.laydate;
        //执行实例

        if (data == "") {
            $("#update").html("添加");
            $("#update").click(function () {
                let code = $("#code").val();
                let name = $("#name").val();
                let desc = $("#desc").val();
                if (code == '') {
                    layer.msg("角色编码不能为空", function () {
                        $("#code").focus();
                    })
                } else if (name == '') {
                    layer.msg("角色名不能为空", function () {
                        $("#name").focus();
                    })
                } else {
                    let roledata = {
                        "code": code,
                        "name": name,
                        "description": desc
                    };
                    console.log(roledata);
                    let result = ajaxModel("/role/role/addRole", "post", roledata);
                    if (result.code == 200) {
                        layer.alert(result.message, {icon: 1});
                        parent.location.reload();
                        parent.layer.close(index);

                    } else
                        layer.alert(result.message, {icon: 1});
                }
            });
            $("#cancel").click(function () {
                parent.layer.close(index);
            });
        } else {
            data = JSON.parse(data);
            $("#code").val(data.code);
            $("#name").val(data.name);
            $("#desc").val(data.description);
            $("#update").click(function () {
                let code = $("#code").val();
                let name = $("#name").val();
                let desc = $("#desc").val();
                if (code == '') {
                    layer.msg("角色编号不能为空", function () {
                        $("#code").focus();
                    })
                } else if (name == '') {
                    layer.msg("角色名不能为空", function () {
                        $("#name").focus();
                    })
                } else {
                    // let result = myAjax("/permission/checkPermission", "get", {"username": code});
                    // if (result.data == null) {
                    let roledata = {
                        "id": data.id,
                        "code": code,
                        "name": name,
                        "description": desc
                    };
                    console.log(roledata);
                    let result = ajaxModel("/role/role/updateRole", "post", roledata);
                    if (result.code == 200) {
                        layer.alert(result.message, {icon: 1});
                        parent.location.reload();
                        parent.layer.close(index);

                    } else
                        layer.alert(result.message, {icon: 1});
                }
            });
            $("#cancel").click(function () {
                parent.layer.close(index);
            });
            form.render();
        }
        form.render();

    });
}

//获取码表数据

function getPermission() {
    let data = null;
    if (sessionStorage.getItem("permission") != null) {
        data = JSON.parse(sessionStorage.getItem("permission"));
    } else {
        let result = ajaxModel("/permission/permission/getAllPermissions", "get");
        data = result.data;
        sessionStorage.setItem("permission", JSON.stringify(data));
    }
    $("#parent_permission").html("<option value=0>暂不选择</option>");
    $.each(data, function (i, permission) {
        $("#parent_permission").append("<option value='" + permission.id + "'>" + permission.name + "</option>");
    });
}